﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using Model;
using Model.Linqtosql;
using System.IO;
using System.Data.OleDb;
using System.Data;
namespace HealthSystem.sysAdmin
{
    public partial class standard_lungs : BasePage
    {
        DataDataContext data = LinqFactory.CreateInstance();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick += Window1.GetShowReference("standard_New.aspx?standardcode=lungs", "标准升级");
                BindVersion();
                BindGrid();
            }
        }
        protected void Grid1_PageIndexChanged(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        public void BindGrid()
        {
            String sversion = ddlversion.SelectedValue.Trim();
            var list = data.standard_lungs.Where(p => p.version == sversion).OrderBy(p => p.age);
            Grid1.RecordCount = list.Count();
            Grid1.DataSource = list.Skip(Grid1.PageIndex * Grid1.PageSize).Take(Grid1.PageSize).ToList();
            Grid1.DataBind();

        }
        public void BindVersion()
        {
            var list = data.standard_lungs.OrderByDescending(p => p.version).Select(p => p.version).Distinct().ToList();
            ddlversion.DataSource = list;
            ddlversion.DataBind();

        }
        protected void ddlversion_Changed(object sender, EventArgs e)
        {
            string sversion = ddlversion.SelectedValue.Trim();
            if (!string.IsNullOrEmpty(sversion))
            {
                var list = data.standard_lungs.Where(p => p.version == sversion).OrderBy(p => p.age).ToList();
                Grid1.RecordCount = list.Count();
                Grid1.DataSource = list;
                Grid1.DataBind();
            }
        }
        protected void btnexport_Click(object sender, EventArgs e)
        {
            var list = data.standard_lungs.Where(p=>p.version==ddlversion.SelectedValue.Trim()).OrderBy(p => p.age).ToList();
            string[] HeaderName = { "年龄", "性别", "P10", "P25", "P75", "P90", "版本" };
            string[] columns = new string[HeaderName.Length];
            DataTable result = new DataTable();
            for (int i = 0; i < HeaderName.Length; i++)
            {
                result.Columns.Add(HeaderName[i], typeof(string));
            }

            DataRow dr, drPre;
            drPre = result.NewRow();
            foreach (var model in list)
            {
                dr = result.NewRow();
                dr[0] = model.age;
                dr[1] = model.sex;
                dr[2] = model.p10;
                dr[3] = model.p25;
                dr[4] = model.p75;
                dr[5] = model.p90;
                dr[6] = model.version;
                result.Rows.Add(dr);

                //克隆一个对象，用于下一行网点性质的对比	linjd	2013-03-06
                drPre = result.NewRow();
                drPre.ItemArray = (object[])dr.ItemArray.Clone();
            }

            //导出Excel表
            //ExportExcel(result);
            string downloadUrl = GetExcelPath(result);
            if (downloadUrl == "")
            {
                ExtAspNet.Alert.Show("生成Excel文件出错！");
            }
            else
            {
                Response.Redirect(downloadUrl);
            }
        }
        public string GetExcelPath(System.Data.DataTable dtData)
        {
            return SaveExcel(dtData, "肺活量标准");
        }
    }
}